package edu.hit.crawler.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import edu.hit.crawler.io.CrawItem;

public class CrawItemComparator implements Comparator<CrawItem> {

	@Override
	public int compare(CrawItem o1, CrawItem o2) {
		String u1 = o1.getUrl();
		String u2 = o2.getUrl();
		int hash1 = hash(u1.getBytes(), 0, u1.getBytes().length);
		int hash2 = hash(u2.getBytes(), 0, u2.getBytes().length);
		return (hash1 < hash2 ? -1 : (hash1 == hash2 ? 0 : 1));
	}

	private static int hash(byte[] bytes, int start, int length) {
		int hash = 1;
		// make later bytes more significant in hash code, so that sorting
		// by
		// hashcode correlates less with by-host ordering.
		for (int i = length - 1; i >= 0; i--)
			hash = (31 * hash) + (int) bytes[start + i];
		return hash;
	}
	public static void main(String []args) throws IOException {
		BufferedReader br = new BufferedReader(new FileReader("in"));
		BufferedWriter bw = new BufferedWriter(new FileWriter("out"));

		String line;
		List<CrawItem> list = new ArrayList<CrawItem>();
		while ( (line = br.readLine()) != null) {
			
		
			line = line.split(" ")[1];
			list.add(new CrawItem(line, CrawItem.SUCCESS));
		}
		Collections.sort(list, new CrawItemComparator());

		for (CrawItem it : list) {
			bw.write(it.getUrl());
			bw.write("\n");
		}
	}
}
